package com.kotiln.lgc.lib_base.adapter

import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView

/**
 *@fileName CommonAdapter
 *@date2021/1/15  12:31
 *@author CommonAdapter$
 *@describe TODO
 *@desc RecyclerView 封装Adapter 万能适配器
 */
open class CommonAdapter<T>:RecyclerView.Adapter<CommonViewHolder>{

    //数据源
    private lateinit var mList:List<T>

    //接口
    private  var  onBindDataListener: OnBindDataListener<T>?=null
    private  var onMoreBindDataListener: OnMoreBindDataListener<T>?=null

    constructor(mList:List<T>,onBindDataListener: OnBindDataListener<T>){
        this.mList=mList
        this.onBindDataListener=onBindDataListener
    }

    constructor(mList: List<T>,onMoreBindDataListener: OnMoreBindDataListener<T>){
        this.mList=mList
        this.onBindDataListener=onMoreBindDataListener
        this.onMoreBindDataListener=onMoreBindDataListener
    }

    //加载布局
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CommonViewHolder {
            val layoutId:Int?=onBindDataListener?.getLayoutId(viewType)
            return CommonViewHolder.getViewHolder(parent,layoutId!!)
    }

    //对RecyclerView子项进行数据赋值
    override fun onBindViewHolder(holder: CommonViewHolder, position: Int) {
         onBindDataListener?.onBindViewHolder(mList[position],holder,getItemViewType(position),position)
    }

    override fun getItemCount(): Int {
        return mList.size
    }

    override fun getItemViewType(position: Int): Int {
        if(onMoreBindDataListener!=null){
            return onMoreBindDataListener!!.getItemType(position)
        }
        return 0
    }

    interface OnBindDataListener<T>{
        fun onBindViewHolder(model:T,viewHolder:CommonViewHolder,type:Int,position: Int)
        fun getLayoutId(type:Int):Int
    }

    interface OnMoreBindDataListener<T>:OnBindDataListener<T>{
        fun getItemType(position:Int):Int
    }
}